package com.weikuang.fareast.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.weikuang.fareast.R;
import com.weikuang.fareast.bean.SortOutBean;

import cn.dlc.commonlibrary.ui.adapter.BaseRecyclerAdapter;


/**
 * 分拣
 */
public class SortOutAdapter extends BaseRecyclerAdapter<SortOutBean> {
    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_sort_out;
    }

    public SortOutAdapter() {

    }

    @Override
    public void onBindViewHolder(CommonHolder holder, int position) {
        SortOutBean bean = getItem(position);

      //  holder.getText(R.id.offic).setText(bean.offic);
       // holder.getText(R.id.hospital).setText(bean.hospital);
        holder.getText(R.id.name).setText(bean.name);
        holder.getText(R.id.fold_nums).setText(bean.fold_nums);
     //   holder.getText(R.id.time).setText(bean.time);


        EditText mExceptionEditText = holder.getView(R.id.ed_num);

        if (mExceptionEditText.getTag(R.id.ed_num) instanceof TextWatcher){
            mExceptionEditText.removeTextChangedListener(((TextWatcher)mExceptionEditText.getTag(R.id.ed_num)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.nums == 0) {
            mExceptionEditText.setText("");
        }else {
            mExceptionEditText.setText(bean.nums + "");
        }
        mExceptionEditText.setSelection(mExceptionEditText.getText().length());

        TextWatcher textWatcher=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.nums = Integer.valueOf(s.toString());
                }
            }
        };
        //设置tag为TextWatcher
        mExceptionEditText.addTextChangedListener(textWatcher);
        mExceptionEditText.setTag(R.id.ed_num,textWatcher);


        EditText mExceptionEditText_loss = holder.getView(R.id.ed_num_loss);

        if (mExceptionEditText_loss.getTag(R.id.ed_num_loss) instanceof TextWatcher){
            mExceptionEditText_loss.removeTextChangedListener(((TextWatcher)mExceptionEditText_loss.getTag(R.id.ed_num_loss)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.num_loss == 0) {
            mExceptionEditText_loss.setText("");
        }else {
            mExceptionEditText_loss.setText(bean.num_loss + "");
        }
        mExceptionEditText_loss.setSelection(mExceptionEditText_loss.getText().length());

        TextWatcher textWatcher_loss=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.num_loss= Integer.valueOf(s.toString());
                }
            }
        };
        //设置tag为TextWatcher
        mExceptionEditText_loss.addTextChangedListener(textWatcher_loss);
        mExceptionEditText_loss.setTag(R.id.ed_num_loss,textWatcher_loss);

//        EditText mExceptionEditText_backs = holder.getView(R.id.ed_num_backs);
//
//        if (mExceptionEditText_backs.getTag(R.id.ed_num_backs) instanceof TextWatcher){
//            mExceptionEditText_backs.removeTextChangedListener(((TextWatcher)mExceptionEditText_backs.getTag(R.id.ed_num_backs)));
//        }
//        //移除了TextWatcher事件后设置item对应的文本
//        mExceptionEditText_backs.setText(bean.backs+"");
//        mExceptionEditText_backs.setSelection(mExceptionEditText_backs.getText().length());
//        TextWatcher textWatcher_backs=new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if (s.equals("0")){
//
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (s.toString().length() > 0) {
//                    bean.backs= Integer.valueOf(s.toString());
//                }
//            }
//        };
//        //设置tag为TextWatcher
//        mExceptionEditText_backs.addTextChangedListener(textWatcher_backs);
//        mExceptionEditText_backs.setTag(R.id.ed_num_backs,textWatcher_backs);
    }
}
